//
//  SystemSetRootViewController.m
//  CPICMobileServiceApp
//
//  Created by sinosoft on 13-2-28.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "SystemSetRootViewController.h"
#import "ShareAccountViewController.h"
#import "SkinSettingViewController.h"
#import "FeedbackViewController.h"
#import "AboutUsViewController.h"
#import "AppDelegate.h"
#import "ManagerSettingRootViewController.h"
#import "UIImageView+WebCache.h"
#import "Config.h"
#import "RCAlertViewUtil.h"

@interface SystemSetRootViewController ()

@end

@implementation SystemSetRootViewController
@synthesize TableViewCustom,TableViewSystem;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title=@"系统设置";
        //self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773@2x"]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
    NSString *bgimage= [accountDefaults objectForKey:@"UserDefaultBgKindKey"];
    
    if ([bgimage isEqualToString:@"蓝色主题风格"])
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88@2x.png");
        ADD_LEFT_BUTTONITEM(@"back.png");
        ADD_TITLE_NAMEITEM(@"系统设置");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773@2x"]];
    }
    else if([bgimage isEqualToString:@"棕色主题风格"])
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88_01@2x.png");
        ADD_LEFT_BUTTONITEM(@"back_01.png");
        ADD_TITLE_NAMEITEM(@"系统设置");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773_01@2x"]];
    }
    else if([bgimage isEqualToString:@"重大节日皮肤"])
    {
        NSData *dataImg_02=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_02];
        NSLog(@"-------找到%@",dataImg_02);
        NSData *dataImg_03=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_03];
        NSData *dataImg_04=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_04];
        
        NSURL *urlSkinImg_02=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_02]];
        NSURL *urlSkinImg_03=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_03]];
        NSURL *urlSkinImg_04=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_04]];
        NSLog(@"-------找到333%@",urlSkinImg_02);
        
        UIImageView * LogoImage = [[UIImageView alloc]init];
        [LogoImage setImageWithURL:urlSkinImg_03 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(LogoImage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_03];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        LogoImage.image = [UIImage imageWithData:dataImg_03];
        LogoImage.frame = CGRectMake(0, 0, LogoImage.image.size.width/2, LogoImage.image.size.height/2);
        [self.view addSubview:LogoImage];
        
        //--------------------------
        UIButton * CallOutBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        CallOutBtn1.frame = CGRectMake(10, 10, 109/2, 57/2);
        UIImageView * Back = [[UIImageView alloc]init];
        [Back setImageWithURL:urlSkinImg_02 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(Back.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_02];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [Back setImage:[UIImage imageWithData:dataImg_02]];
        [CallOutBtn1 setImage:Back.image forState:UIControlStateNormal];
        [CallOutBtn1 addTarget:self action:@selector(ClickCallOutBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:CallOutBtn1];
        //--------------------------
        
        ADD_TITLE_NAMEITEM(@"系统设置");
        
        UIImageView *bgimage = [[UIImageView alloc]initWithFrame:CGRectMake(0,44, self.view.frame.size.width, self.view.frame.size.height-44)];
        [bgimage setImageWithURL:urlSkinImg_04 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(bgimage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_04];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [bgimage setImage:[UIImage imageWithData:dataImg_04]];
        [self.view addSubview:bgimage];
        [bgimage release];
    }
    
    //添加UITableview
    TableViewSystem = [[UITableView alloc]initWithFrame:CGRectMake(15, 70, 290, 50) style:UITableViewStylePlain];
	TableViewSystem.delegate = self;
	TableViewSystem.dataSource = self;
    
    TableViewSystem.opaque = NO;
    TableViewSystem.backgroundView = nil;
    TableViewSystem.autoresizingMask = UIViewContentModeTop;
    TableViewSystem.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    TableViewSystem.scrollEnabled=NO;
    //TableViewSystem.backgroundColor=[UIColor clearColor];
    //[TableViewUser reloadData];
	[self.view addSubview:TableViewSystem];
    
    
    TableViewCustom = [[UITableView alloc]initWithFrame:CGRectMake(15, 120, 290, 100) style:UITableViewStylePlain];
	TableViewCustom.delegate = self;
	TableViewCustom.dataSource = self;
    
    TableViewCustom.opaque = NO;
    TableViewCustom.backgroundView = nil;
    TableViewCustom.autoresizingMask = UIViewContentModeTop;
    TableViewCustom.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    TableViewCustom.scrollEnabled=NO;
    //TableViewCustom.backgroundColor=[UIColor clearColor];
    //[TableViewPassword reloadData];
	[self.view addSubview:TableViewCustom];
    
    arrSystem=[[NSMutableArray alloc]initWithObjects:@"分享账号绑定", nil];
    arrCustom=[[NSMutableArray alloc]initWithObjects:@"意见反馈",@"关于我们", nil];

}

-(void) ClickCallOutBtn
{
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView==TableViewSystem)
    {
        return [arrSystem count];
    }
    else
    {
        return [arrCustom count];
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView==TableViewSystem)
    {
        static NSString *Identifier=@"Identifier";
        UITableViewCell *cell=(UITableViewCell *)[TableViewSystem dequeueReusableCellWithIdentifier:Identifier];
        if (cell==nil)
        {
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]autorelease];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;//选中后颜色没变化
        }
     
            UILabel *lblUserAccount=[[UILabel alloc]initWithFrame:CGRectMake(65, 15, 130, 21)];
            lblUserAccount.backgroundColor=[UIColor clearColor];
            lblUserAccount.text=[arrSystem objectAtIndex:indexPath.row];
            [cell addSubview:lblUserAccount];
            [lblUserAccount release];
            
        UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 97/2, 88/2)];
//        if (indexPath.row==0) {
//            imgView.image=[UIImage imageNamed:@"SysSet_01"];
//            [cell addSubview:imgView];
//        }
//        else if(indexPath.row==1)
//        {
//            imgView.image=[UIImage imageNamed:@"SysSet_02"];
//            [cell addSubview:imgView];
//        }
        if(indexPath.row==0)
        {
            imgView.image=[UIImage imageNamed:@"SysSet_03"];
            [cell addSubview:imgView];
        }

            return cell;
    }
    else
    {
        static NSString *Identifier=@"Identifier";
        UITableViewCell *cell=(UITableViewCell *)[TableViewCustom dequeueReusableCellWithIdentifier:Identifier];
        if (cell==nil)
        {
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]autorelease];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }

            UILabel *lblUserPassword=[[UILabel alloc]initWithFrame:CGRectMake(65, 15, 130, 21)];
            lblUserPassword.backgroundColor=[UIColor clearColor];
            lblUserPassword.text=[arrCustom objectAtIndex:indexPath.row];
            [cell addSubview:lblUserPassword];
            [lblUserPassword release];
         
        UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 97/2, 88/2)];
        if (indexPath.row==0) {
            imgView.image=[UIImage imageNamed:@"SysSet_04"];
            [cell addSubview:imgView];
        }
//        else if(indexPath.row==1)
//        {
//            imgView.image=[UIImage imageNamed:@"SysSet_05"];
//            [cell addSubview:imgView];
//        }
        else if(indexPath.row==1)
        {
            imgView.image=[UIImage imageNamed:@"SysSet_06"];
            [cell addSubview:imgView];
        }
        
        return cell;
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView==TableViewSystem)
    {
//        if(indexPath.row==0)
//        {
//            SkinSettingViewController *skinSettingViewCtrl=[[SkinSettingViewController alloc]init];
//            [[AppDelegate app].unc pushViewController:skinSettingViewCtrl animated:YES];
//            [skinSettingViewCtrl release];
//        }
        if(indexPath.row==0)
        {
            ShareAccountViewController *shareAccountViewCtrl=[[ShareAccountViewController alloc]init];
            [self.navigationController pushViewController:shareAccountViewCtrl animated:YES];
            [ShareAccountViewController release];
        }
    }
    else if(tableView==TableViewCustom)
    {
        if (indexPath.row==0) {
            FeedbackViewController *feedbackViewCtrl=[[FeedbackViewController alloc]init];
            [self.navigationController pushViewController:feedbackViewCtrl animated:YES];
            [feedbackViewCtrl release];
        }
//        else if(indexPath.row==1)
//        {
//            ManagerSettingRootViewController *managerSettingRootViewCtrl=[[ManagerSettingRootViewController alloc]init];
//            [self.navigationController pushViewController:managerSettingRootViewCtrl animated:YES];
//            [managerSettingRootViewCtrl release];
//        }
        else if(indexPath.row==1)
        {
            AboutUsViewController *aboutUsViewCtrl=[[AboutUsViewController alloc]init];
            [self.navigationController pushViewController:aboutUsViewCtrl animated:YES];
            [aboutUsViewCtrl release];
        }
    }
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)dealloc
{
    [arrCustom release];
    [arrSystem release];
    [TableViewCustom release];
    [TableViewSystem release];
    [super dealloc];
}
@end
